﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConnectionToSQL;
using System.Xml;
using System.Data.SqlClient;
using System.Data;
using System.IO;
using System.Windows.Forms;


namespace quanlynhansu.DAO
{
    class Connection
    {
        public string connectionString;
        public int count;
        public SqlConnection connect;
        public Connection()
        {
            string FileConfig = Application.StartupPath + "\\configConnectString.xml";
            FileStream fs = new FileStream(FileConfig, FileMode.Open);
            XmlTextReader r = new XmlTextReader(fs);
            while (r.Read())
            {
                //kiem tra thuoc tinh id
                if (r.NodeType == XmlNodeType.Element)
                {
                    if (r.LocalName.Equals("count"))
                    {
                        count = Convert.ToInt32(r.ReadString());
                    }
                    //node connectionString
                    if (r.LocalName.Equals("connectString"))
                    {
                        connectionString = r.ReadString();
                    }
                }
            }
            r.Close();
            connect = new SqlConnection(connectionString);

        }
        public Connection(string conString)
        {
            this.connectionString = conString;
            this.connect = new SqlConnection(connectionString);
        }
        public bool KiemTra()
        {
            try
            {
                this.connect.Open();
                this.connect.Close();
                return true;
            }
            catch
            {
                string pathMdf = Application.StartupPath + "\\ictu_database.mdf";
                string pathLdf = Application.StartupPath + "\\ictu_database.ldf";
                this.Attach("ictu_database", pathMdf, pathLdf);
                return false;
            }
        }
        public void Open()
        {
            this.connect.Open();
        }
        public void Close()
        {
            this.connect.Close();
        }
        public void Attach(string databaseName, string pathMdf, string pathLdf)
        {
            string con = @"Data Source=.\SQLEXPRESS; Initial catalog=master Integrated Security=true;";
            SqlConnection c = new SqlConnection(con);
            SqlParameter p1 = new SqlParameter("@Name", databaseName);
            SqlParameter p2 = new SqlParameter("@Path1", pathMdf);
            SqlParameter p3 = new SqlParameter("@Path2", pathLdf);
            string query = "EXEC sp_attach_db @Name,@Path1,@Path2";
            //mo ket noi
            c.Open();
            SqlCommand cmd = new SqlCommand(query, c);
            cmd.Parameters.Add(p1);
            cmd.Parameters.Add(p2);
            cmd.Parameters.Add(p3);
            cmd.ExecuteNonQuery();
            c.Close();

        }
    }



}
